/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author Frank Gabriel Rodríguez Lee
 */

public class UtilesJdbc {
    public static ResultSet getDatos( String query, Connection cn ) throws Exception{
        Statement st = cn.createStatement();          
        ResultSet rs = st.executeQuery(query);        
        return rs;        
    }
    
    public static List convierteLista( ResultSet rs ) throws Exception{
        List arr = new ArrayList( 1 );

        List temp = null;
        ResultSetMetaData rmd = null;
        int ncamp = 0;

        rmd = rs.getMetaData();
        ncamp = rmd.getColumnCount();

        while( rs.next() ){
            temp = new ArrayList( 1 );
            for( int i = 1; i <= ncamp; i++ ){
                temp.add( rs.getObject( i ) );
            }
            arr.add( temp );
        }
        return arr;            
    }


    public static List convierteListaTabla( ResultSet rs ) throws Exception{
        List arr = new ArrayList( 1 );
        List etiquetas = new ArrayList( 1 );
        List clases = new ArrayList( 1 );

        List temp = null;
        ResultSetMetaData rmd = null;
        int ncamp = 0;

        rmd = rs.getMetaData();
        ncamp = rmd.getColumnCount();

        for( int i = 0; i < ncamp; i++ ){
            clases.add( Class.forName( rmd.getColumnClassName( i + 1 ) ) );
            etiquetas.add( rmd.getColumnLabel( i + 1 ) );
        }
        while( rs.next() ){
            temp = new ArrayList( 1 );
            for( int i = 1; i <= ncamp; i++ ){
                temp.add( rs.getObject( i ) );
            }
            arr.add( temp );
        }
        arr.add( clases );
        arr.add( etiquetas );
        return arr;
    }
}
